package com.ruoyi.erecord.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.tools.enums.DeviceTypeEnum;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erecord.device.domain.Device;
import com.ruoyi.erecord.device.domain.bo.DeviceBo;
import com.ruoyi.erecord.device.domain.bo.DeviceImportDetailBo;
import com.ruoyi.erecord.device.domain.vo.DeviceImportDetailVo;
import com.ruoyi.erecord.device.domain.vo.DeviceVo;
import com.ruoyi.erecord.device.mapper.DeviceMapper;
import com.ruoyi.erecord.device.service.IDeviceImportDetailService;
import com.ruoyi.erecord.device.service.IDeviceService;
import com.ruoyi.erecord.employee.service.IEmployeeService;
import com.ruoyi.erecord.vehicles.service.IVehiclesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 设备Service业务层处理
 *
 * @author cc
 * @date 2023-04-19
 */
@RequiredArgsConstructor
@Service
public class DeviceServiceImpl implements IDeviceService {

    private final DeviceMapper baseMapper;

    private final IDeviceImportDetailService deviceImportDetailService;

    private final IEmployeeService iEmployeeService;
    private final IVehiclesService iVehiclesService;

    /**
     * 查询设备
     */
    @Override
    public DeviceVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设备列表
     */
    @Override
    public TableDataInfo<DeviceVo> queryPageList(DeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Device> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(Device::getCreateTime);
        Page<DeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备列表
     */
    @Override
    public List<DeviceVo> queryList(DeviceBo bo) {
        LambdaQueryWrapper<Device> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Device> buildQueryWrapper(DeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Device> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), Device::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getSimCode()), Device::getSimCode, bo.getSimCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Device::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getBrand()), Device::getBrand, bo.getBrand());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), Device::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Device::getName, bo.getName());
        //        lqw.orderByDesc(Device::getCreateTime);
        return lqw;
    }

    @Override
    public List<Device> queryByList(List<Device> list){
        return baseMapper.queryByList(list);
    }

    /**
     * 新增设备
     */
    @Override
    public Boolean insertByBo(DeviceBo bo) {
        Device add = BeanUtil.toBean(bo, Device.class);

        validEntityBeforeSave(add);

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备
     */
    @Override
    public Boolean updateByBo(DeviceBo bo) {
        Device update = BeanUtil.toBean(bo, Device.class);
        //validEntityBeforeSave(update);
        return baseMapper.updateInfo(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Device entity) {
        String code = Optional.ofNullable(entity).map(Device::getCode).orElse("0");
        int count = this.queryByCode(code);
        if (count > 0) {
            throw new ServiceException("设备编码 已存在！");
        }
        //        验证编码是佛存在 end
    }

    /**
     * 批量删除设备
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }

        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsertOrUpdate(String batchNo) {
        DeviceImportDetailBo queryBo = new DeviceImportDetailBo();
        queryBo.setBatchNo(batchNo);

        List<DeviceImportDetailVo> detailList = deviceImportDetailService.queryList(queryBo);
        List<Device> list = BeanCopyUtils.copyList(detailList, Device.class);
        if (list != null) {
            list.forEach(BaseEntity::restBase);
            return this.baseMapper.insertOrUpdateBatch(list);
        }
        return false;
    }

    @Override
    public boolean hasLocus(Collection<String> ids) {
        return this.baseMapper.hasLocus(ids) > 0;
    }

    @Override
    public int queryByCode(String code) {
        return this.baseMapper.queryByCode(code);
    }

    @Override
    public boolean isUseById(DeviceTypeEnum typeEnum, Long id) {
        LambdaQueryWrapper<Device> lqw = Wrappers.lambdaQuery();
        lqw.eq(Device::getType, typeEnum.getCode());
        lqw.eq(Device::getDeviceId, id);
        return this.baseMapper.exists(lqw);
    }
}
